<?php
namespace app\admin\controller;

use app\admin\model\MemberFlow;
use app\admin\controller\Export;
use think\Db;

class Member extends Common
{
    /**
     * 用户列表
     */
    public function lists(){
        $list = model('Admin')->index();
        $page = $list->render();

        // 模板变量赋值
        $this->assign('list',$list);
        $this->assign('page',$page);

        // 渲染模板输出
        return $this->fetch();
    }

    /**
    *  添加用户
    */
    public function add(){

        return $this->fetch();
    }

    /**
    * 修改会员账号
    */
    public function info(){
        $id = input('id');

        if(request()->isPost()){ 
            $mobile = input('mobile');

            $member_result = db('Member')->where(['mobile'=>$mobile])->find();

            if(!empty($member_result))$this->error('该账号已注册!');

            db('Member')->where(['id'=>$id])->update(['mobile'=>$mobile]);

            $this->redirect("Member/lst");
        }

        $member = db('member')->where(['id'=>$id])->find();
        $this->assign('member',$member);
        $this->assign('id',$id);
        return $this->fetch();
    }

    /**
     * 会员列表
     */
    public function lst(){
        $keyword = input('keyword');
        $is_excls = input('is_excls');
        $where = [];
        if(!empty($keyword)){
            $where['mobile'] = ['like',"%{$keyword}%"];
        }

        if(!empty($is_excls)){
            $this->export();exit;
        }

        $members = db("member")
            ->where($where)
            ->order("id desc")
            ->paginate(10);
    
        $this -> assign('page',$members->render());
        $this -> assign('members',$members);
        $this -> assign('keyword',$keyword);
        return $this -> fetch();
    }

    /**
    * 会员操作
    */
    public function member_edit(){
        $edit = input('edit');
        $status = input('status');
        $id = input('id');

        $member = db('member')->where(['id'=>$id])->find();

        if(empty($member))$this->error('id有误!');

        if($edit == 'status'){
            db('member')->where(['id'=>$id])->update(['status'=>$status]);
        }

        if($edit == 'del'){
            db('member')->where(['id'=>$id])->delete();
        }

        $this->success('修改成功');
    }

    /**
     * 充值余额
     */
    public function recharge(){
        $id = input('get.id');
        $members = DB::name('member') -> where('id',$id) -> field('id,amount') -> find();
        $this -> assign('members',$members);
        return $this -> fetch();
    }

    /**
     * 充值余额
     */
    public function doRecharge(){

        // 验证参数
        $validate = new \app\admin\validate\Member;
        $validate -> doRecharge();
        $res = $validate -> goCheck();
        if($res !== true){
            $this -> error($res);
        }

        $param = input('post.');
        $member = new \app\admin\model\Member;
        $res = $member -> recharge($param['id'],$param['money']);
        if(!$res){
            $this -> error('充值失败');
        }
        $this -> success('充值成功','lst');
    }


    /**
     * 查看充值记录
     */
    public function record(){
        $id = input('id');
        $flowModel = new MemberFlow();
        $flows = $flowModel::where('uid','=',$id) -> paginate(config('common_page'));
        $list = [];
        foreach ($flows as $flow){
            $list[] = $flow -> toArray();
        }
        $this -> assign('list',$list);
        $this -> assign('page',$flows -> render());
        return $this -> fetch();
    }

    //修改密码
    public function change_password(){
        $id = input('id');

        if(request()->isPost()){ 
            $password = input('password');
            $config_password = input('config_password');

            if($password != $config_password)$this->error('两次密码不一样！');

            db('Member')->where(['id'=>$id])->update(['password'=>md5($password)]);

            $this->redirect("Member/lst");
        }

        $member = db('member')->where(['id'=>$id])->find();
        $this->assign('member',$member);
        return $this->fetch();
    }

    /**
     * 商户导出excel
     */
    public function export()
    {

        $headArr = array("ID","手机号码","企业名","联系人","用户所在行业","用户所在地区","固定电话","qq","邮件",'金额（余额）');
        $list = db("member")->order("id desc")->select();
        $data=array();
        foreach($list as $k => $v){
            $data[$k][] = $v['id'];
            $data[$k][] = $v['mobile'];
            $data[$k][] = $v['company'];
            $data[$k][] = $v['contact'];
            $data[$k][] = $v['user_industry'];
            $data[$k][] = $v['users_location'];
            $data[$k][] = $v['telephone'];
            $data[$k][] = $v['qq'];
            $data[$k][] = $v['email'];
            $data[$k][] = $v['amount'];
        }
        $filename='会员列表-'.date("Y-m-d H:i:s");
        (new Export())->ImportExcel($filename,$headArr,$data);
    }
}
